package horse;
public class horse
{
	int razmer=5;	
	static void print_mass(int [][]mass, int a, int b)
	{
		for (int i=0; i<a; i++)
		{
			for (int j=0; j<b; j++)
				System.out.print(mass[i][j] + "\t");
			System.out.print("\n");
		}
		System.out.print("\n");
	}
	static int count=0;		
	
	private void horse_move (int [][]arr, int level, int str, int poz)
	{
		//System.out.print("level = " + level +" str= "+ str +" poz = "+poz+ "\n" );
		arr[str][poz]=level;
		if(level == razmer*razmer)
		{
			print_mass(arr, razmer, razmer);
			count++;
		  // System.out.print("count = "+ count + "\n");
		}
		else
		{
			if (str+2<razmer && poz+1<razmer && 					    arr[str+2][poz+1]==0)horse_move(arr, level+1, str+2, poz+1);
			if (str+2<razmer && poz-1<razmer && poz-1>=0 &&  		    arr[str+2][poz-1]==0)horse_move(arr, level+1, str+2, poz-1);
			if (str+1<razmer && poz+2<razmer && 		    		    arr[str+1][poz+2]==0)horse_move(arr, level+1, str+1, poz+2);
			if (str+1<razmer && poz-2<razmer && poz-2>=0 && 			arr[str+1][poz-2]==0)horse_move(arr, level+1, str+1, poz-2);
			if (str-2<razmer && poz-1<razmer && poz-1>=0 && str-2>=0 && arr[str-2][poz-1]==0)horse_move(arr, level+1, str-2, poz-1);
			if (str-2<razmer && poz+1<razmer && str-2>=0 && 			arr[str-2][poz+1]==0)horse_move(arr, level+1, str-2, poz+1);
			if (str-1<razmer && poz+2<razmer && str-1>=0 &&  		    arr[str-1][poz+2]==0)horse_move(arr, level+1, str-1, poz+2);
			if (str-1<razmer && poz-2<razmer && poz-2>=0 && str-1>=0 && arr[str-1][poz-2]==0)horse_move(arr, level+1, str-1, poz-2);
		}
		
		arr[str][poz]=0; // íå ïîïàëè íå â îäíî èç óñëîâèé âûøå - ýòîò õîä ïðèâîäèò â òóïèê 
		//System.out.print("level = " + level + "\n");	
	}	
	public static void main(String[] args)
	{
		horse a= new horse();
		int[][] arr = new int[a.razmer][a.razmer];
		for (int i=0; i<a.razmer; i++)
			for (int j=0; j<a.razmer; j++)
				a.horse_move(arr, 1, i, j);
		System.out.print("the number of all possible  " + count );
	}
}